﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace persistencia
{
    public class ConBD
    {
        private OleDbConnection conexReal;
        private OleDbDataReader dataR;
        private static string defaultFormatConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        

        public OleDbConnection ConexReal
        {
            get { return conexReal; }
            set { conexReal = value; }
        }
        public OleDbDataReader DataR
        {
            get { return dataR; }
            set { dataR = value; }
        }

        public void conectarse(string path) 
        {
            try
            {
                if (conexReal == null)
                {

                    conexReal = new OleDbConnection(defaultFormatConnection + path);
                    conexReal.Open();
                }

            }
            catch (Exception e1)
            {
                throw new System.ArgumentException("No se pudo conectar a la Base de datos");
            }
        }

        public void desconectarse()
        {
            try
            {
                if (conexReal != null)
                    conexReal.Close();
            }
            catch (Exception e) 
            {
                throw new System.ArgumentException("No se pudo desconectar de la Base de datos");
            }
        }
        public void ejecutarse(string sentencia)
        {
            try
            {
                OleDbCommand comm = new OleDbCommand(sentencia,conexReal);
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new System.ArgumentException("Falló al ejecutarse la consulta");
            }
        }

        public void traerDatos(string sentencia)
        {
            try
            {
                OleDbCommand comm = conexReal.CreateCommand();
                comm.CommandText = sentencia;
                dataR= comm.ExecuteReader();
            }
            catch (Exception e)
            {

            }
        }

      
        public void finConsulta()
        {
            try
            {
                if (dataR != null) 
                {
                    dataR.Close();
                }
            }
            catch (Exception e)
            {

            }
        }



        public object obtenerValor(int i)
        {
            try
            {
                if (dataR != null && (!dataR.IsClosed))
                {
                    return dataR[i];
                }
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public object obtenerValor(string nombre)
        {
            try
            {
                if (dataR != null && (!dataR.IsClosed))
                {
                    return dataR[nombre];
                }
                return null;
            }
            catch (Exception e)
            {
                return null;
            }
        }

    }
}
